package com.example.bilibili.controller;

import com.example.bilibili.entity.Buser;
import com.example.bilibili.entity.GroupEntity;
import com.example.bilibili.service.BuserService;
import com.example.bilibili.utils.Node;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Buser)表控制层
 *
 * @author makejava
 * @since 2022-08-16 20:29:29
 */
@RestController
@RequestMapping("buser")
@CrossOrigin(origins = "*")
public class BuserController {
    /**
     * 服务对象
     */
    @Resource
    private BuserService buserService;

    /**
     * 分页查询
     *
     * @param buser 筛选条件
     * @return 查询结果
     */
    @GetMapping("getList")
    public ResponseEntity<Page<Buser>> queryByPage(Buser buser,
                                                   @RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "10") int size,
                                                   @RequestParam(defaultValue = "id") String sort, boolean asc) {
        PageRequest pageRequest = PageRequest.of(page, size, asc ? Sort.Direction.ASC : Sort.Direction.DESC, sort);
        return ResponseEntity.ok(this.buserService.queryByPage(buser, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("getOne")
    public ResponseEntity<Buser> queryById(String id) {
        return ResponseEntity.ok(this.buserService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param buser 实体
     * @return 新增结果
     */
    @PostMapping("insert")
    public ResponseEntity<Buser> add(Buser buser) {
        return ResponseEntity.ok(this.buserService.insert(buser));
    }

    /**
     * 编辑数据
     *
     * @param buser 实体
     * @return 编辑结果
     */
    @PutMapping("update")
    public ResponseEntity<Buser> edit(Buser buser) {
        return ResponseEntity.ok(this.buserService.update(buser));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("delete")
    public ResponseEntity<Boolean> deleteById(String id) {
        return ResponseEntity.ok(this.buserService.deleteById(id));
    }

    @PostMapping("/group")
    public ResponseEntity<List<Node>> group(@RequestBody GroupEntity group) {
        return ResponseEntity.ok(buserService.group(group));
    }
}

